package com.ruoyi.web.controller.inventory;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.asset.service.IAssetDetailService;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.inventory.domain.LostScrap;
import com.ruoyi.inventory.domain.LostScrapDetail;
import com.ruoyi.inventory.service.ILostScrapService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 遗失报废Controller
 * 
 * @author ruoyi
 * @date 2020-07-01
 */
@Controller
@RequestMapping("/inventory/scrap")
public class LostScrapController extends BaseController
{
    private String prefix = "inventory";

    @Autowired
    private ILostScrapService lostScrapService;
    @Autowired
    private IAssetDetailService assetDetailService;

    @RequiresPermissions("inventory:scrap:view")
    @GetMapping()
    public String scrap()
    {
        return prefix + "/scrap";
    }

    /**
     * 查询遗失报废列表
     */
    @RequiresPermissions("inventory:scrap:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(LostScrapDetail lostScrapDetail)
    {
        startPage();
        List<LostScrapDetail> list = lostScrapService.selectLostScrapDetailList(lostScrapDetail);
        return getDataTable(list);
    }

    /**
     * 导出遗失报废列表
     */
    @RequiresPermissions("inventory:scrap:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(LostScrapDetail lostScrapDetail)
    {
        List<LostScrapDetail> list = lostScrapService.selectLostScrapDetailList(lostScrapDetail);
        ExcelUtil<LostScrapDetail> util = new ExcelUtil<LostScrapDetail>(LostScrapDetail.class);
        return util.exportExcel(list, "遗失报废信息");
    }


    /**
     * 添加遗失处理
     */
    @Log(title = "添加遗失处理", businessType = BusinessType.INSERT)
    @PostMapping("/lost")
    @ResponseBody
    public AjaxResult lost(LostScrap lostScrap)
    {
        List<LostScrap> lostScrapList=lostScrapService.selectLostScrapList(lostScrap);
        if (CollectionUtils.isEmpty(lostScrapList)){
            lostScrap.setCreateBy(ShiroUtils.getUserId().toString());
            lostScrap.setDelFlag("0");
            lostScrap.setStatus("1");
            return toAjax(lostScrapService.insertLostScrap(lostScrap));
        }else{
            return AjaxResult.error("该资产已处理！");
        }
    }

    /**
     * 添加报废处理
     */
    @Log(title = "添加报废处理", businessType = BusinessType.INSERT)
    @PostMapping( "/scrap")
    @ResponseBody
    public AjaxResult scrap(LostScrap lostScrap)
    {
        List<LostScrap> lostScrapList=lostScrapService.selectLostScrapList(lostScrap);
        if (CollectionUtils.isEmpty(lostScrapList)){
            lostScrap.setCreateBy(ShiroUtils.getUserId().toString());
            lostScrap.setDelFlag("0");
            lostScrap.setStatus("2");
            return toAjax(lostScrapService.insertLostScrap(lostScrap));
        }else{
            return AjaxResult.error("该资产已处理！");
        }
    }

    /**
     * 批量遗失处理
     */
    @Log(title = "批量遗失处理", businessType = BusinessType.INSERT)
    @PostMapping("/lostAll")
    @ResponseBody
    public AjaxResult lostAll(LostScrap lostScrap)
    {
        Long assetId=lostScrap.getAssetId();
        String[] inventoryNoArray=lostScrap.getInventoryNo().split(",");
        List<LostScrap> lostScrapList=new ArrayList<>();
        for (String inventoryNo:inventoryNoArray) {
            LostScrap lostScrapParam=new LostScrap();
            lostScrapParam.setInventoryNo(inventoryNo);
            lostScrapParam.setAssetId(assetId);
            lostScrapParam.setStatus("1");
            lostScrapParam.setCreateBy(ShiroUtils.getUserId().toString());
            lostScrapList.add(lostScrapParam);
        }
       return toAjax(lostScrapService.insertLostScraps(lostScrapList));
    }

    /**
     * 批量报废处理
     */
    @Log(title = "批量报废处理", businessType = BusinessType.INSERT)
    @PostMapping( "/scrapAll")
    @ResponseBody
    public AjaxResult scrapAll(LostScrap lostScrap)
    {
        Long assetId=lostScrap.getAssetId();
        String[] inventoryNoArray=lostScrap.getInventoryNo().split(",");
        List<LostScrap> lostScrapList=new ArrayList<>();
        for (String inventoryNo:inventoryNoArray) {
            LostScrap lostScrapParam=new LostScrap();
            lostScrapParam.setInventoryNo(inventoryNo);
            lostScrapParam.setAssetId(assetId);
            lostScrapParam.setStatus("2");
            lostScrapParam.setCreateBy(ShiroUtils.getUserId().toString());
            lostScrapList.add(lostScrapParam);
        }
        return toAjax(lostScrapService.insertLostScraps(lostScrapList));
    }
}
